import pyautogui
import time
import 手势动作模块_动作判断_双手模式_握拳版 as motion

motion = motion.MotionDetection()


class HandAction():
    def __init__(self):
        self.flag = "None"

    # 分配左右手
    def action(self, type, lmList):
        if len(lmList) == 0:
            return
        # 握拳
        if motion.makeAFist(lmList) and self.flag != "makeAFist":
            self.flag = "makeAFist"
            print("握拳")
            # self.releaseAllKey()
        elif type == "Left":
            self.left(lmList)
        else:
            self.right(lmList)

    # 右手操作
    def right(self, lmList):
        if motion.indexFinger(lmList) and self.flag != "rightIndexFinger":
            print("左方向键（左转)(右食指)")
            self.flag = "rightIndexFinger"
            pyautogui.keyDown('left')
        elif motion.loseIndexFinger(lmList) and self.flag != "loseIndexFinger":
            self.flag = "loseIndexFinger"
            pyautogui.keyUp('left')
        elif motion.loseLittleFinger(lmList) and self.flag != "loseLittleFinger":
            self.flag = "loseLittleFinger"
            pyautogui.keyUp('right')
        elif motion.middleFinger(lmList) and self.flag != "rightMiddleFinger":
            self.flag = "rightMiddleFinger"
            print("上方向键(小喷)(中指)")
            pyautogui.press('up')
        elif motion.littleFinger(lmList) and self.flag != "littleFinger":
            self.flag = "littleFinger"
            print("右方向键(右转)(小拇指)")
            pyautogui.keyDown('right')


    # 左手操作
    def left(self, lmList):
        if motion.indexFinger(lmList) and self.flag != "leftIndexFinger":
            self.flag = "leftIndexFinger"
            print("漂移(shift)(左手食指)")
            pyautogui.keyDown('shift')
        elif motion.loseIndexFinger(lmList) and self.flag != "loseIndexFinger":
            self.flag = "loseIndexFinger"
            pyautogui.keyUp('shift')
            pyautogui.press("left")
            pyautogui.press("right")

        elif motion.littleFinger(lmList) and self.flag != "leftIndexFinger":
            self.flag = "leftIndexFinger"
            print("氮气加速(ctrl)(小拇指)")
            pyautogui.press('ctrl')

    def releaseAllKey(self):
        pyautogui.keyUp('shift')
        pyautogui.keyUp('left')
        pyautogui.keyUp('right')

    # # 共同操作
    # def coOperation(self, lmList):
    #     if self.littleFinger(lmList):
    #         print("氮气加速(ctrl)(小拇指)")
    #         pyautogui.press('ctrl')
    #     elif self.middleFinger(lmList):
    #         print("上方向键(小喷)(中指)")
    #         pyautogui.press('up')
    #     elif self.ringFinger(lmList):
    #         print("R(复原)(无名指)")
    #         pyautogui.press('R')
    #     else:
    #         print("不做事情")

    # 动作判断
    # 因为使用opencv的方向，高度是从左上角算的。左上角是0,0,这与现实世界是不同的
    # 握拳
    def makeAFist(self, lmList):
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] < lmList[14][1] and
                lmList[20][1] < lmList[18][1] and lmList[4][1] < lmList[2][1]):
            return True
        return False

    # 食指
    def indexFinger(self, lmList):
        # 1指向右转
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] > lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            return True
        return False

    # 中指和食指
    def middleFingerAndIndexFinger(self, lmList):
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            return True
        return False

    # 中指
    def middleFinger(self, lmList):
        if (lmList[8][1] > lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            return True
        return False

    # 小拇指
    def littleFinger(self, lmList):
        if (lmList[8][1] > lmList[6][1] and lmList[12][1] > lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] < lmList[18][1]):
            return True
        return False

    # 无名指
    def ringFinger(self, lmList):
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] < lmList[18][1]):
            return True
        return False

    # 开掌
    def openPalm(self, lmList):
        if (lmList[8][1] > lmList[6][1] and lmList[12][1] > lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            return True
        return False
